﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
/// <summary>
///Helper 的摘要说明
/// </summary>
public class Helper
{
    public static void Alert(string mes)
    {
        string script = "<script>alert ('" + mes + "')</script>";
        HttpContext.Current.Response.Write(script);
        Literal lit = new Literal();
        lit.Text = script;
        Page page = HttpContext.Current.Handler as Page;
        page.Controls.Add(lit);
    }
    public static void Alert(string mes,string scriptKey)
    {
        string script = "alert ('" + mes + "')";
        //HttpContext.Current.Response.Write(script);
        //Literal lit = new Literal();
        //lit.Text = script;
        //Page page = HttpContext.Current.Handler as Page;
        //page.Controls.Add(lit);
        WriteScriptAtBottom(script, scriptKey);
    }
    public static void Alert(string mes, string scriptKey, int backstep)
    {
        string script = "alert ('" + mes + "');history.go("+backstep+")";
        WriteScriptAtBottom(script, scriptKey);
    }
    public static void Alert(string mes,string scriptKey, string url)
    {
        string script = "alert ('" + mes + "');location.href='"+url+"';";
        //HttpContext.Current.Response.Write(script);
        //Literal lit = new Literal();
        //lit.Text = script;
        //Page page = HttpContext.Current.Handler as Page;
        //page.Controls.Add(lit);
        WriteScriptAtBottom(script, scriptKey);
    }
    public static string mCheck(CheckBoxList cbl)
    {
        string r = string.Empty;
        foreach (ListItem item in cbl.Items)
        {
            if (item.Selected)
            {
                if (!string.IsNullOrEmpty(r))
                {
                    r += ",";
                }
                r += item.Value;
            }
        }
        return r;
    }

    //跳转页面
    public void setUrl(string url)
    {
 
    }

    public static void WriteScriptAtTop(string script,string scriptKey)
    {
        Page page = HttpContext.Current.Handler as Page;
        if (page != null)
        {
            if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), scriptKey))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), scriptKey, "<script language='javascript'>" + script + "</script>");
            }
        }
    }
    public static void WriteScriptAtBottom(string script,string scriptKey)
    {
        Page page = HttpContext.Current.Handler as Page;
        if (page != null)
        {
            if (!page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), scriptKey))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), scriptKey, "<script language=javascript>" + script + "</script>");
            }
        }
    }

    public static void WriteScriptByLit(string script)
    {
        Page page = HttpContext.Current.Handler as Page;
        if (page != null)
        {
            Literal lit = new Literal();
            lit.Text = "<script language=javascript>" + script + "</script>";
            page.Page.Controls.AddAt(0, lit);
        }
    }
}
